﻿using System;
using System.Windows.Forms;
using MultipleChoice_ExamGenerator.multiplechoicedbDataSetTableAdapters;
using MultipleChoice_ExamGenerator.VariousUtils;
using MySql.Data.MySqlClient;

namespace MultipleChoice_ExamGenerator
{
    public partial class RegisterForm : Form
    {
        private readonly AuthorValidator _authorValidator;

        public RegisterForm()
        {
            InitializeComponent();

            _authorValidator = new AuthorValidator();

            // Αλλάζουμε το format της ημερομηνίας
            dobDateTimePicker.Format = DateTimePickerFormat.Custom;
            dobDateTimePicker.CustomFormat = "dd/MM/yyyy";
        }

        private void registerButton_Click(object sender, EventArgs e)
        {
            if (IsValid())
            {
                try
                {
                    var adapter = new authorsTableAdapter();
                    adapter.InsertAuthor(usernameTB.Text, passwordTB1.Text,
                        firstNameTB.Text, lastNameTB.Text, emailTB.Text,
                        sQuestionCB.SelectedItem.ToString(), sAnswerTB.Text,
                        dobDateTimePicker.Value,
                        genderComboBox.SelectedItem.ToString());

                    MessageBox.Show("Registration completed!",
                        "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Close();
                }
                catch (MySqlException mySqlException)
                {
                    MessageBox.Show(mySqlException.Message,
                    "Error!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Please enter valid data!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Με αυτή την μέθοδο ελέγχουμε αν τα πεδία της φόρμας
        /// είναι σωστά συμπληρωμένα
        /// </summary>
        /// <returns>true/false</returns>
        private bool IsValid()
        {
            return _authorValidator.ValidateUsername(usernameTB, errorProvider1)
                   & _authorValidator.ValidatePassword(passwordTB1, passwordTB2, errorProvider1)
                   & _authorValidator.ValidateSecurityQuestion(sQuestionCB, errorProvider1)
                   & _authorValidator.ValidateRequired(firstNameTB, errorProvider1)
                   & _authorValidator.ValidateRequired(lastNameTB, errorProvider1)
                   & _authorValidator.ValidateRequired(sAnswerTB, errorProvider1)
                   & _authorValidator.ValidateEmail(emailTB, errorProvider1)
                   & _authorValidator.ValidateGender(genderComboBox, errorProvider1);
        }

        private void passwordTB2_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _authorValidator.ValidatePassword(passwordTB1, passwordTB2, errorProvider1);
        }

        private void passwordTB1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _authorValidator.ValidatePassword(passwordTB1, passwordTB2, errorProvider1);
        }

        private void emailTB_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _authorValidator.ValidateEmail(emailTB, errorProvider1);
        }

        private void usernameTB_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _authorValidator.ValidateUsername(usernameTB, errorProvider1);
        }

        private void firstNameTB_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _authorValidator.ValidateRequired(firstNameTB, errorProvider1);
        }

        private void lastNameTB_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _authorValidator.ValidateRequired(lastNameTB, errorProvider1);
        }

        private void sQuestionCB_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _authorValidator.ValidateSecurityQuestion(sQuestionCB, errorProvider1);
        }

        private void sAnswerTB_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _authorValidator.ValidateRequired(sAnswerTB, errorProvider1);
        }

        private void genderGroup_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _authorValidator.ValidateGender(genderComboBox, errorProvider1);
        }
    }
}
